package com.zjj.concurrent.sync;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/**
 * @author zhanglei.zjj
 * @description 测试偏向锁是否会重偏向
 * @date 2023/11/2 1:10
 */
@Slf4j
public class ExtrovertLockDestroy {
    public static void main(String[] args) throws InterruptedException {
        // 偏向锁延迟偏向
        Thread.sleep(5000);
        Object lock = new Object();
        log.debug(ClassLayout.parseInstance(lock).toPrintable());

        new Thread(() -> ExtrovertLockDestroy.syncMethod(lock)).start();

        // 睡眠5s，保证线程1已经执行完临界区代码，线程2才启动
        Thread.sleep(5000);
        new Thread(() -> ExtrovertLockDestroy.syncMethod(lock)).start();
    }

    private  static void syncMethod(Object lock) {
        synchronized (lock) {
            log.debug(ClassLayout.parseInstance(lock).toPrintable());
            System.out.println("线程-" + Thread.currentThread().getName() + "执行临界区代码....");
        }
    }
}
